<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="python基础,">










<meta name="description" content="巩固python基础">
<meta name="keywords" content="python基础">
<meta property="og:type" content="article">
<meta property="og:title" content="python巩固">
<meta property="og:url" content="http://Anzusen.com/2019/03/30/python巩固/index.html">
<meta property="og:site_name" content="Anzhusen">
<meta property="og:description" content="巩固python基础">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="http://www.runoob.com/wp-content/uploads/2013/11/python-string-slice.png">
<meta property="og:image" content="http://www.runoob.com/wp-content/uploads/2013/11/python_list_slice_2.png">
<meta property="og:image" content="http://www.runoob.com/wp-content/uploads/2013/11/list_slicing1.png">
<meta property="og:updated_time" content="2019-09-11T11:55:05.455Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="python巩固">
<meta name="twitter:description" content="巩固python基础">
<meta name="twitter:image" content="http://www.runoob.com/wp-content/uploads/2013/11/python-string-slice.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://Anzusen.com/2019/03/30/python巩固/">





  <title>python巩固 | Anzhusen</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>
    <a href="https://github.com/ananzhusen/ananzhusen.github.io" class="github-corner" aria-label="View source on GitHub"><svg width="80" height="80" viewbox="0 0 250 250" style="fill:#64CEAA; color:#fff; position: absolute; top: 0; border: 0; right: 0;" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"/><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"/><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"/></svg></a><style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Anzhusen</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br>
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off" placeholder="搜索..." spellcheck="false" type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://Anzusen.com/2019/03/30/python巩固/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="ananzhusen">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/images/touxiang.JPG">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Anzhusen">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">python巩固</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-03-30T13:43:31+08:00">
                2019-03-30
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/python/" itemprop="url" rel="index">
                    <span itemprop="name">python</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2019/03/30/python巩固/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/2019/03/30/python巩固/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          
            <span class="post-meta-divider">|</span>
            <span class="page-pv">本文总阅读量 <i class="fa fa-file-o"></i>
            <span class="busuanzi-value" id="busuanzi_value_page_pv"></span>次
            </span>
          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  8.5k
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  32
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h3 id="巩固python基础"><a href="#巩固python基础" class="headerlink" title="巩固python基础"></a>巩固python基础</h3><a id="more"></a>
<p>ps: 该篇博客学习自<a href="https://www.runoob.com/python/python-tutorial.html" target="_blank" rel="noopener">菜鸟教程</a></p>
<h4 id="中文编码"><a href="#中文编码" class="headerlink" title="中文编码"></a>中文编码</h4><p>Python中默认的编码格式是 ASCII 格式，在没修改编码格式时无法正确打印汉字，所以在读取中文时会报错。<br>解决方法为只要在文件开头加入 <strong># -*- coding: UTF-8 -*-</strong> 或者 <strong>#coding=utf-8</strong> 就行了</p>
<blockquote>
<p><strong>注意：**</strong>#coding=utf-8<strong> 的 </strong>=** 号两边不要空格。</p>
</blockquote>
<h4 id="标识符"><a href="#标识符" class="headerlink" title="标识符"></a>标识符</h4><ul>
<li><p>以下划线开头的标识符是有特殊意义的。以单下划线开头 <strong>_foo</strong> 的代表不能直接访问的类属性，需通过类提供的接口进行访问，不能用 <strong>from xxx import *</strong> 而导入。</p>
</li>
<li><p>以双下划线开头的 <strong>__foo</strong> 代表类的私有成员，以双下划线开头和结尾的 <strong><strong>foo</strong></strong> 代表 Python 里特殊方法专用的标识，如 <strong><strong>init</strong>()</strong> 代表类的构造函数。</p>
<h4 id="行和缩进"><a href="#行和缩进" class="headerlink" title="行和缩进"></a>行和缩进</h4><p>学习 Python 与其他语言最大的区别就是，Python 的代码块不使用大括号 {} 来控制类，函数以及其他逻辑判断。python 最具特色的就是用缩进来写模块。<br>缩进的空白数量是可变的，但是所有代码块语句必须包含相同的缩进空白数量，这个必须严格执行。</p>
<h4 id="多行语句"><a href="#多行语句" class="headerlink" title="多行语句"></a>多行语句</h4><p>Python语句中一般以新行作为语句的结束符。</p>
</li>
</ul>
<p>但是我们可以使用斜杠（ \）将一行的语句分为多行显示，如下所示：<br><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">total = item_one + \</span><br><span class="line">        item_two + \</span><br><span class="line">        item_three</span><br></pre></td></tr></table></figure></p>
<p>语句中包含 [], {} 或 () 括号就不需要使用多行连接符。</p>
<h4 id="Print-输出"><a href="#Print-输出" class="headerlink" title="Print 输出"></a>Print 输出</h4><p>print 默认输出是换行的，如果要实现不换行需要在变量末尾加上逗号 ,</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># -*- coding: UTF-8 -*-</span></span><br><span class="line"></span><br><span class="line">x=<span class="string">"a"</span></span><br><span class="line">y=<span class="string">"b"</span></span><br><span class="line"><span class="comment"># 换行输出</span></span><br><span class="line"><span class="keyword">print</span> x</span><br><span class="line"><span class="keyword">print</span> y</span><br><span class="line"><span class="keyword">print</span> <span class="string">'---------'</span></span><br><span class="line"><span class="comment"># 不换行输出</span></span><br><span class="line"><span class="keyword">print</span> x,</span><br><span class="line"><span class="keyword">print</span> y,</span><br><span class="line"><span class="comment"># 不换行输出</span></span><br><span class="line"><span class="keyword">print</span> x,y</span><br></pre></td></tr></table></figure>
<p>以上实例执行结果为：</p>
<blockquote>
<p>a</p>
<h2 id="b"><a href="#b" class="headerlink" title="b"></a>b</h2><p>a b a b</p>
</blockquote>
<h4 id="标准数据类型"><a href="#标准数据类型" class="headerlink" title="标准数据类型"></a>标准数据类型</h4><p>Python有五个标准的数据类型：</p>
<ul>
<li>Numbers（数字）</li>
<li>String（字符串）</li>
<li>List（列表）</li>
<li>Tuple（元组）</li>
<li>Dictionary（字典）</li>
</ul>
<h5 id="数字"><a href="#数字" class="headerlink" title="数字"></a>数字</h5><ul>
<li>长整型也可以使用小写 l，但是还是建议您使用大写 L，避免与数字 1 混淆。Python使用 L 来显示长整型。</li>
<li><p>Python 还支持复数，复数由实数部分和虚数部分构成，可以用 a + bj,或者 complex(a,b) 表示， 复数的实部 a 和虚部 b 都是浮点型。<br><strong>注意</strong>：long 类型只存在于 Python2.X 版本中，在 2.2 以后的版本中，int 类型数据溢出后会自动转为long类型。在 Python3.X 版本中 long 类型被移除，使用 int 替代。</p>
<h5 id="字符串"><a href="#字符串" class="headerlink" title="字符串"></a>字符串</h5><p>python的字串列表有2种取值顺序:</p>
</li>
<li><p>从左到右索引默认0开始的，最大范围是字符串长度少1</p>
</li>
<li>从右到左索引默认-1开始的，最大范围是字符串开头<br><img src="http://www.runoob.com/wp-content/uploads/2013/11/python-string-slice.png" alt="取值顺序"><h6 id="实例"><a href="#实例" class="headerlink" title="实例"></a>实例</h6><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># -*- coding: UTF-8 -*-</span></span><br><span class="line"> </span><br><span class="line">str = <span class="string">'Hello World!'</span></span><br><span class="line"> </span><br><span class="line"><span class="keyword">print</span> str           <span class="comment"># 输出完整字符串</span></span><br><span class="line"><span class="keyword">print</span> str[<span class="number">0</span>]        <span class="comment"># 输出字符串中的第一个字符</span></span><br><span class="line"><span class="keyword">print</span> str[<span class="number">2</span>:<span class="number">5</span>]      <span class="comment"># 输出字符串中第三个至第五个之间的字符串</span></span><br><span class="line"><span class="keyword">print</span> str[<span class="number">2</span>:]       <span class="comment"># 输出从第三个字符开始的字符串</span></span><br><span class="line"><span class="keyword">print</span> str * <span class="number">2</span>       <span class="comment"># 输出字符串两次</span></span><br><span class="line"><span class="keyword">print</span> str + <span class="string">"TEST"</span>  <span class="comment"># 输出连接的字符串</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
<p>以上实例输出结果：</p>
<blockquote>
<p>Hello World!<br>H<br>llo<br>llo World!<br>Hello World!Hello World!<br>Hello World!TEST</p>
</blockquote>
<ul>
<li>Python 列表截取可以接收第三个参数，参数作用是截取的步长，以下实例在索引 1 到索引 4 的位置并设置为步长为 2（间隔一个位置）来截取字符串：<br><img src="http://www.runoob.com/wp-content/uploads/2013/11/python_list_slice_2.png" alt="间隔取值"></li>
</ul>
<h5 id="列表"><a href="#列表" class="headerlink" title="列表"></a>列表</h5><p>List（列表） 是 Python 中使用最频繁的数据类型。<br>列表可以完成大多数集合类的数据结构实现。它支持字符，数字，字符串甚至可以包含列表（即嵌套）。<br>列表用 [ ] 标识，是 python 最通用的复合数据类型。<br><img src="http://www.runoob.com/wp-content/uploads/2013/11/list_slicing1.png" alt="列表"></p>
<h6 id="列表方法"><a href="#列表方法" class="headerlink" title="列表方法"></a>列表方法</h6><table>
<thead>
<tr>
<th style="text-align:left">序号</th>
<th style="text-align:left">方法</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">1</td>
<td style="text-align:left"><a href="http://www.runoob.com/python/att-list-append.html" target="_blank" rel="noopener">list.append(obj)</a> 在列表末尾添加新的对象</td>
</tr>
<tr>
<td style="text-align:left">2</td>
<td style="text-align:left"><a href="http://www.runoob.com/python/att-list-count.html" target="_blank" rel="noopener">list.count(obj)</a> 统计某个元素在列表中出现的次数</td>
</tr>
<tr>
<td style="text-align:left">3</td>
<td style="text-align:left"><a href="http://www.runoob.com/python/att-list-extend.html" target="_blank" rel="noopener">list.extend(seq)</a> 在列表末尾一次性追加另一个序列中的多个值（用新列表扩展原来的列表）</td>
</tr>
<tr>
<td style="text-align:left">4</td>
<td style="text-align:left"><a href="http://www.runoob.com/python/att-list-index.html" target="_blank" rel="noopener">list.index(obj)</a> 从列表中找出某个值第一个匹配项的索引位置</td>
</tr>
<tr>
<td style="text-align:left">5</td>
<td style="text-align:left"><a href="http://www.runoob.com/python/att-list-insert.html" target="_blank" rel="noopener">list.insert(index, obj)</a> 将对象插入列表</td>
</tr>
<tr>
<td style="text-align:left">6</td>
<td style="text-align:left"><a href="http://www.runoob.com/python/att-list-pop.html" target="_blank" rel="noopener">list.pop([index=-1])</a> 移除列表中的一个元素（默认最后一个元素），并且返回该元素的值</td>
</tr>
<tr>
<td style="text-align:left">7</td>
<td style="text-align:left"><a href="http://www.runoob.com/python/att-list-remove.html" target="_blank" rel="noopener">list.remove(obj)</a> 移除列表中某个值的第一个匹配项</td>
</tr>
<tr>
<td style="text-align:left">8</td>
<td style="text-align:left"><a href="http://www.runoob.com/python/att-list-reverse.html" target="_blank" rel="noopener">list.reverse()</a> 反向列表中元素</td>
</tr>
<tr>
<td style="text-align:left">9</td>
<td style="text-align:left"><a href="http://www.runoob.com/python/att-list-sort.html" target="_blank" rel="noopener">list.sort(cmp=None, key=None, reverse=False)</a> 对原列表进行排序</td>
</tr>
</tbody>
</table>
<h6 id="实例-1"><a href="#实例-1" class="headerlink" title="实例"></a>实例</h6><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># -*- coding: UTF-8 -*- </span></span><br><span class="line"><span class="comment"># 列表</span></span><br><span class="line">vowels = [<span class="string">'e'</span>, <span class="string">'a'</span>, <span class="string">'u'</span>, <span class="string">'o'</span>, <span class="string">'i'</span>]</span><br><span class="line"><span class="comment"># 降序</span></span><br><span class="line">vowels.sort(reverse=<span class="literal">True</span>)</span><br><span class="line"><span class="comment"># 输出结果</span></span><br><span class="line"><span class="keyword">print</span> <span class="string">'降序输出:'</span>, vowel</span><br></pre></td></tr></table></figure>
<p>以上实例输出结果如下：</p>
<blockquote>
<p>降序输出: [‘u’, ‘o’, ‘i’, ‘e’, ‘a’]</p>
</blockquote>
<h5 id="元组"><a href="#元组" class="headerlink" title="元组"></a>元组</h5><p>元组是另一个数据类型，类似于 List（列表）。<br>元组用 , 标识。内部元素用逗号隔开。<strong>但是元组不能二次赋值</strong>，相当于只读列表。</p>
<ul>
<li><p>创建空元组</p>
<blockquote>
<p>tup1 = ()</p>
</blockquote>
</li>
<li><p>元组中只包含一个元素时，需要在元素后面添加逗号</p>
<blockquote>
<p>tup1 = (50,)</p>
</blockquote>
</li>
</ul>
<h6 id="实例-2"><a href="#实例-2" class="headerlink" title="实例"></a>实例</h6><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># -*- coding: UTF-8 -*-</span></span><br><span class="line"></span><br><span class="line">tuple = ( <span class="string">'runoob'</span>, <span class="number">786</span> , <span class="number">2.23</span>, <span class="string">'john'</span>, <span class="number">70.2</span> )</span><br><span class="line">tinytuple = (<span class="number">123</span>, <span class="string">'john'</span>)</span><br><span class="line"> </span><br><span class="line"><span class="keyword">print</span> tuple               <span class="comment"># 输出完整元组</span></span><br><span class="line"><span class="keyword">print</span> tuple[<span class="number">0</span>]            <span class="comment"># 输出元组的第一个元素</span></span><br><span class="line"><span class="keyword">print</span> tuple[<span class="number">1</span>:<span class="number">3</span>]          <span class="comment"># 输出第二个至第三个的元素 </span></span><br><span class="line"><span class="keyword">print</span> tuple[<span class="number">2</span>:]           <span class="comment"># 输出从第三个开始至列表末尾的所有元素</span></span><br><span class="line"><span class="keyword">print</span> tinytuple * <span class="number">2</span>       <span class="comment"># 输出元组两次</span></span><br><span class="line"><span class="keyword">print</span> tuple + tinytuple   <span class="comment"># 打印组合的元组</span></span><br></pre></td></tr></table></figure>
<p>以上实例输出结果：</p>
<blockquote>
<p>(‘runoob’, 786, 2.23, ‘john’, 70.2)<br>runoob<br>(786, 2.23)<br>(2.23, ‘john’, 70.2)<br>(123, ‘john’, 123, ‘john’)<br>(‘runoob’, 786, 2.23, ‘john’, 70.2, 123, ‘john’)</p>
</blockquote>
<h5 id="字典"><a href="#字典" class="headerlink" title="字典"></a>字典</h5><p>字典(dictionary)是除列表以外python之中最灵活的内置数据结构类型。列表是有序的对象集合，字典是无序的对象集合。<br>两者之间的区别在于：字典当中的元素是通过键来存取的，而不是通过偏移存取。<br>字典用”{ }”标识。字典由索引(key)和它对应的值value组成。</p>
<h6 id="删除字典元素"><a href="#删除字典元素" class="headerlink" title="删除字典元素"></a>删除字典元素</h6><p>####### 实例<br><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># -*- coding: UTF-8 -*-</span></span><br><span class="line">dict = &#123;<span class="string">'Name'</span>: <span class="string">'Zara'</span>, <span class="string">'Age'</span>: <span class="number">7</span>, <span class="string">'Class'</span>: <span class="string">'First'</span>&#125;</span><br><span class="line"> </span><br><span class="line"><span class="keyword">del</span> dict[<span class="string">'Name'</span>]  <span class="comment"># 删除键是'Name'的条目</span></span><br><span class="line">dict.clear()      <span class="comment"># 清空词典所有条目</span></span><br><span class="line"><span class="keyword">del</span> dict          <span class="comment"># 删除词典</span></span><br></pre></td></tr></table></figure></p>
<h6 id="内置方法："><a href="#内置方法：" class="headerlink" title="内置方法："></a>内置方法：</h6><table>
<thead>
<tr>
<th style="text-align:left">序号</th>
<th style="text-align:left">函数及描述</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">1</td>
<td style="text-align:left"><a href="http://www.runoob.com/python/att-dictionary-clear.html" target="_blank" rel="noopener">dict.clear()</a> 删除字典内所有元素</td>
</tr>
<tr>
<td style="text-align:left">2</td>
<td style="text-align:left"><a href="http://www.runoob.com/python/att-dictionary-copy.html" target="_blank" rel="noopener">dict.copy()</a> 返回一个字典的浅复制</td>
</tr>
<tr>
<td style="text-align:left">3</td>
<td style="text-align:left"><a href="http://www.runoob.com/python/att-dictionary-fromkeys.html" target="_blank" rel="noopener">dict.fromkeys(seq[, val])</a> 创建一个新字典，以序列 seq 中元素做字典的键，val 为字典所有键对应的初始值</td>
</tr>
<tr>
<td style="text-align:left">4</td>
<td style="text-align:left"><a href="http://www.runoob.com/python/att-dictionary-get.html" target="_blank" rel="noopener">dict.get(key, default=None)</a> 返回指定键的值，如果值不在字典中返回default值</td>
</tr>
<tr>
<td style="text-align:left">5</td>
<td style="text-align:left"><a href="http://www.runoob.com/python/att-dictionary-has_key.html" target="_blank" rel="noopener">dict.has_key(key)</a> 如果键在字典dict里返回true，否则返回false</td>
</tr>
<tr>
<td style="text-align:left">6</td>
<td style="text-align:left"><a href="http://www.runoob.com/python/att-dictionary-items.html" target="_blank" rel="noopener">dict.items()</a> 以列表返回可遍历的(键, 值) 元组数组</td>
</tr>
<tr>
<td style="text-align:left">7</td>
<td style="text-align:left"><a href="http://www.runoob.com/python/att-dictionary-keys.html" target="_blank" rel="noopener">dict.keys()</a> 以列表返回一个字典所有的键</td>
</tr>
<tr>
<td style="text-align:left">8</td>
<td style="text-align:left"><a href="http://www.runoob.com/python/att-dictionary-setdefault.html" target="_blank" rel="noopener">dict.setdefault(key, default=None)</a> 和get()类似, 但如果键不存在于字典中，将会添加键并将值设为default</td>
</tr>
<tr>
<td style="text-align:left">9</td>
<td style="text-align:left"><a href="http://www.runoob.com/python/att-dictionary-update.html" target="_blank" rel="noopener">dict.update(dict2)</a> 把字典dict2的键/值对更新添加到dict里</td>
</tr>
<tr>
<td style="text-align:left">10</td>
<td style="text-align:left"><a href="http://www.runoob.com/python/att-dictionary-values.html" target="_blank" rel="noopener">dict.values()</a> 以列表返回字典中的所有值</td>
</tr>
<tr>
<td style="text-align:left">11</td>
<td style="text-align:left"><a href="http://www.runoob.com/python/python-att-dictionary-pop.html" target="_blank" rel="noopener">pop(key[,default])</a> 删除字典给定键 key 所对应的值，返回值为被删除的值。key值必须给出。 否则，返回default值。</td>
</tr>
<tr>
<td style="text-align:left">12</td>
<td style="text-align:left"><a href="http://www.runoob.com/python/python-att-dictionary-popitem.html" target="_blank" rel="noopener">popitem()</a> 随机返回并删除字典中的一对键和值。</td>
</tr>
</tbody>
</table>
<p>####### 实例<br>以下实例展示了 fromkeys()函数的使用方法：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># -*- coding: UTF-8 -*-</span></span><br><span class="line"> </span><br><span class="line">seq = (<span class="string">'Google'</span>, <span class="string">'Runoob'</span>, <span class="string">'Taobao'</span>)</span><br><span class="line"> </span><br><span class="line">dict = dict.fromkeys(seq)</span><br><span class="line"><span class="keyword">print</span> <span class="string">"新字典为 : %s"</span> %  str(dict)</span><br><span class="line"> </span><br><span class="line">dict = dict.fromkeys(seq, <span class="number">10</span>)</span><br><span class="line"><span class="keyword">print</span> <span class="string">"新字典为 : %s"</span> %  str(dict)</span><br></pre></td></tr></table></figure>
<p>以上实例输出结果为：</p>
<blockquote>
<p>新字典为 : {‘Google’: None, ‘Taobao’: None, ‘Runoob’: None}<br>新字典为 : {‘Google’: 10, ‘Taobao’: 10, ‘Runoob’: 10}</p>
</blockquote>
<h6 id="实例-3"><a href="#实例-3" class="headerlink" title="实例"></a>实例</h6><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># -*- coding: UTF-8 -*-</span></span><br><span class="line"> </span><br><span class="line">dict = &#123;&#125;</span><br><span class="line">dict[<span class="string">'one'</span>] = <span class="string">"This is one"</span></span><br><span class="line">dict[<span class="number">2</span>] = <span class="string">"This is two"</span></span><br><span class="line">tinydict = &#123;<span class="string">'name'</span>: <span class="string">'john'</span>,<span class="string">'code'</span>:<span class="number">6734</span>, <span class="string">'dept'</span>: <span class="string">'sales'</span>&#125;</span><br><span class="line"> </span><br><span class="line"><span class="keyword">print</span> dict[<span class="string">'one'</span>]          <span class="comment"># 输出键为'one' 的值</span></span><br><span class="line"><span class="keyword">print</span> dict[<span class="number">2</span>]              <span class="comment"># 输出键为 2 的值</span></span><br><span class="line"><span class="keyword">print</span> tinydict             <span class="comment"># 输出完整的字典</span></span><br><span class="line"><span class="keyword">print</span> tinydict.keys()      <span class="comment"># 输出所有键</span></span><br><span class="line"><span class="keyword">print</span> tinydict.values()    <span class="comment"># 输出所有值</span></span><br></pre></td></tr></table></figure>
<p>输出结果为：</p>
<blockquote>
<p>This is one<br>This is two<br>{‘dept’: ‘sales’, ‘code’: 6734, ‘name’: ‘john’}<br>[‘dept’, ‘code’, ‘name’]<br>[‘sales’, 6734, ‘john’]</p>
</blockquote>
<h4 id="运算符"><a href="#运算符" class="headerlink" title="运算符"></a>运算符</h4><h5 id="算数运算符"><a href="#算数运算符" class="headerlink" title="算数运算符"></a>算数运算符</h5><table>
<thead>
<tr>
<th>**</th>
<th>幂 - 返回x的y次幂</th>
<th>a**b 为10的20次方， 输出结果 100000000000000000000</th>
</tr>
</thead>
<tbody>
<tr>
<td>//</td>
<td>取整除 - 返回商的整数部分（<strong>向下取整</strong>）</td>
<td><code>&gt;&gt;&gt; 9//2 4 &gt;&gt;&gt; -9//2 -5</code></td>
</tr>
</tbody>
</table>
<h5 id="位运算符"><a href="#位运算符" class="headerlink" title="位运算符"></a>位运算符</h5><table>
<thead>
<tr>
<th style="text-align:left">运算符</th>
<th style="text-align:left">描述</th>
<th style="text-align:left">实例</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">&amp;</td>
<td style="text-align:left">按位与运算符：参与运算的两个值,如果两个相应位都为1,则该位的结果为1,否则为0</td>
<td style="text-align:left">(a &amp; b) 输出结果 12 ，二进制解释： 0000 1100</td>
</tr>
<tr>
<td style="text-align:left">&#124;</td>
<td style="text-align:left">按位或运算符：只要对应的二个二进位有一个为1时，结果位就为1。</td>
<td style="text-align:left">(a  &#124; b) 输出结果 61 ，二进制解释： 0011 1101</td>
</tr>
<tr>
<td style="text-align:left">^</td>
<td style="text-align:left">按位异或运算符：当两对应的二进位相异时，结果为1</td>
<td style="text-align:left">(a ^ b) 输出结果 49 ，二进制解释： 0011 0001</td>
</tr>
<tr>
<td style="text-align:left">~</td>
<td style="text-align:left">按位取反运算符：对数据的每个二进制位取反,即把1变为0,把0变为1 。<strong>~x</strong> 类似于 <strong>-x-1</strong></td>
<td style="text-align:left">(~a ) 输出结果 -61 ，二进制解释： 1100 0011，在一个有符号二进制数的补码形式。</td>
</tr>
<tr>
<td style="text-align:left">&lt;&lt;</td>
<td style="text-align:left">左移动运算符：运算数的各二进位全部左移若干位，由 <strong>&lt;&lt;</strong> 右边的数字指定了移动的位数，高位丢弃，低位补0。</td>
<td style="text-align:left">a &lt;&lt; 2 输出结果 240 ，二进制解释： 1111 0000</td>
</tr>
<tr>
<td style="text-align:left">&gt;&gt;</td>
<td style="text-align:left">右移动运算符：把”&gt;&gt;”左边的运算数的各二进位全部右移若干位，<strong>&gt;&gt;</strong> 右边的数字指定了移动的位数</td>
<td style="text-align:left">a &gt;&gt; 2 输出结果 15 ，二进制解释： 0000 1111</td>
</tr>
</tbody>
</table>
<h5 id="逻辑运算符"><a href="#逻辑运算符" class="headerlink" title="逻辑运算符"></a>逻辑运算符</h5><p>Python语言支持逻辑运算符，以下假设变量 a 为 10, b为 20:</p>
<table>
<thead>
<tr>
<th style="text-align:left">运算符</th>
<th style="text-align:left">逻辑表达式</th>
<th style="text-align:left">描述</th>
<th style="text-align:left">实例</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">and</td>
<td style="text-align:left">x and y</td>
<td style="text-align:left">布尔”与” - 如果 x 为 False，x and y 返回 False，否则它返回 y 的计算值。</td>
<td style="text-align:left">(a and b) 返回 20。</td>
</tr>
<tr>
<td style="text-align:left">or</td>
<td style="text-align:left">x or y</td>
<td style="text-align:left">布尔”或”    - 如果 x 是非 0，它返回 x 的值，否则它返回 y 的计算值。</td>
<td style="text-align:left">(a or b) 返回 10。</td>
</tr>
<tr>
<td style="text-align:left">not</td>
<td style="text-align:left">not x</td>
<td style="text-align:left">布尔”非” - 如果 x 为 True，返回 False 。如果 x 为 False，它返回 True。</td>
<td style="text-align:left">not(a and b) 返回 False</td>
</tr>
</tbody>
</table>
<h5 id="成员运算符"><a href="#成员运算符" class="headerlink" title="成员运算符"></a>成员运算符</h5><p>除了以上的一些运算符之外，Python还支持成员运算符，测试实例中包含了一系列的成员，包括字符串，列表或元组。</p>
<table>
<thead>
<tr>
<th style="text-align:left">运算符</th>
<th style="text-align:left">描述</th>
<th style="text-align:left">实例</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">in</td>
<td style="text-align:left">如果在指定的序列中找到值返回 True，否则返回 False。</td>
<td style="text-align:left">x 在 y 序列中 , 如果 x 在 y 序列中返回 True。</td>
</tr>
<tr>
<td style="text-align:left">not in</td>
<td style="text-align:left">如果在指定的序列中没有找到值返回 True，否则返回 False。</td>
<td style="text-align:left">x 不在 y 序列中 , 如果 x 不在 y 序列中返回 True。</td>
</tr>
</tbody>
</table>
<h5 id="身份运算符"><a href="#身份运算符" class="headerlink" title="身份运算符"></a>身份运算符</h5><p>身份运算符用于比较两个对象的存储单元</p>
<table>
<thead>
<tr>
<th style="text-align:left">运算符</th>
<th style="text-align:left">描述</th>
<th style="text-align:left">实例</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">is</td>
<td style="text-align:left">is 是判断两个标识符是不是引用自一个对象</td>
<td style="text-align:left"><strong>x is y</strong>, 类似 <strong>id(x) == id(y)</strong> , 如果引用的是同一个对象则返回 True，否则返回 False</td>
</tr>
<tr>
<td style="text-align:left">is not</td>
<td style="text-align:left">is not 是判断两个标识符是不是引用自不同对象</td>
<td style="text-align:left"><strong>x is not y</strong> ， 类似 <strong>id(a) != id(b)</strong>。如果引用的不是同一个对象则返回结果 True，否则返回 False。</td>
</tr>
</tbody>
</table>
<p><strong>is 与 == 区别：</strong><br>is 用于判断两个变量引用对象是否为同一个， == 用于判断引用变量的值是否相等。</p>
<h5 id="字符串运算符"><a href="#字符串运算符" class="headerlink" title="字符串运算符"></a>字符串运算符</h5><table>
<thead>
<tr>
<th style="text-align:left">运算符</th>
<th style="text-align:left">描述</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">r/R</td>
<td style="text-align:left">原始字符串 - 原始字符串：所有的字符串都是直接按照字面的意思来使用，没有转义特殊或不能打印的字符。 原始字符串除在字符串的第一个引号前加上字母”r”（可以大小写）以外，与普通字符串有着几乎完全相同的语法。</td>
<td>&gt;&gt;&gt;print r’\n’ \n &gt;&gt;&gt; print R’\n’ \n</td>
</tr>
</tbody>
</table>
<h5 id="运算符优先级"><a href="#运算符优先级" class="headerlink" title="运算符优先级"></a>运算符优先级</h5><p>以下表格列出了从最高到最低优先级的所有运算符：</p>
<table>
<thead>
<tr>
<th style="text-align:left">运算符</th>
<th style="text-align:left">描述</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">**</td>
<td style="text-align:left">指数 (最高优先级)</td>
</tr>
<tr>
<td style="text-align:left">~ + -</td>
<td style="text-align:left">按位翻转, 一元加号和减号 (最后两个的方法名为 +@ 和 -@)</td>
</tr>
<tr>
<td style="text-align:left">* / % //</td>
<td style="text-align:left">乘，除，取模和取整除</td>
</tr>
<tr>
<td style="text-align:left">+ -</td>
<td style="text-align:left">加法减法</td>
</tr>
<tr>
<td style="text-align:left">&gt;&gt; &lt;&lt;</td>
<td style="text-align:left">右移，左移运算符</td>
</tr>
<tr>
<td style="text-align:left">&amp;</td>
<td style="text-align:left">位 ‘AND’</td>
</tr>
<tr>
<td style="text-align:left">^ \ &#124;</td>
<td style="text-align:left">位运算符</td>
</tr>
<tr>
<td style="text-align:left">&lt;= &lt; &gt; &gt;=</td>
<td style="text-align:left">比较运算符</td>
</tr>
<tr>
<td style="text-align:left">&lt;&gt; == !=</td>
<td style="text-align:left">等于运算符</td>
</tr>
<tr>
<td style="text-align:left">= %= /= //= -= += *= **=</td>
<td style="text-align:left">赋值运算符</td>
</tr>
<tr>
<td style="text-align:left">is is not</td>
<td style="text-align:left">身份运算符</td>
</tr>
<tr>
<td style="text-align:left">in not in</td>
<td style="text-align:left">成员运算符</td>
</tr>
<tr>
<td style="text-align:left">not and or</td>
<td style="text-align:left">逻辑运算符</td>
</tr>
</tbody>
</table>
<h4 id="语句"><a href="#语句" class="headerlink" title="语句"></a>语句</h4><ul>
<li>python 并不支持 switch 语句，所以多个条件判断，只能用 elif 来实现，如果判断需要多个条件需同时判断时，可以使用 or （或），表示两个条件有一个成立时判断条件成功；使用 and （与）时，表示只有两个条件同时成立的情况下，判断条件才成功。</li>
</ul>
<h5 id="pass-语句"><a href="#pass-语句" class="headerlink" title="pass 语句"></a>pass 语句</h5><p>Python pass 是空语句，是为了保持程序结构的完整性。<br>pass 不做任何事情，一般用做占位语句。</p>
<h6 id="实例："><a href="#实例：" class="headerlink" title="实例："></a>实例：</h6><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># -*- coding: UTF-8 -*- </span></span><br><span class="line"> </span><br><span class="line"><span class="comment"># 输出 Python 的每个字母</span></span><br><span class="line"><span class="keyword">for</span> letter <span class="keyword">in</span> <span class="string">'Python'</span>:</span><br><span class="line">   <span class="keyword">if</span> letter == <span class="string">'h'</span>:</span><br><span class="line">      <span class="keyword">pass</span></span><br><span class="line">      <span class="keyword">print</span> <span class="string">'这是 pass 块'</span></span><br><span class="line">   <span class="keyword">print</span> <span class="string">'当前字母 :'</span>, letter</span><br><span class="line"> </span><br><span class="line"><span class="keyword">print</span> <span class="string">"Good bye!"</span></span><br></pre></td></tr></table></figure>
<p>以上实例执行结果：</p>
<blockquote>
<p>当前字母 : P<br>当前字母 : y<br>当前字母 : t<br>这是 pass 块<br>当前字母 : h<br>当前字母 : o<br>当前字母 : n<br>Good bye!</p>
</blockquote>
<h4 id="数学函数"><a href="#数学函数" class="headerlink" title="数学函数"></a>数学函数</h4><h5 id="math-模块、cmath-模块"><a href="#math-模块、cmath-模块" class="headerlink" title="math 模块、cmath 模块"></a>math 模块、cmath 模块</h5><p>Python 中数学运算常用的函数基本都在 math 模块、cmath 模块中。</p>
<ul>
<li><p>Python math 模块提供了许多对浮点数的数学运算函数。</p>
</li>
<li><p>Python cmath 模块包含了一些用于复数运算的函数。</p>
</li>
<li><p>cmath 模块的函数跟 math 模块函数基本一致，区别是 cmath 模块运算的是复数，math 模块运算的是数学运算。</p>
</li>
</ul>
<p>查看 math 查看包中的内容:<br><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">import</span> math</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>dir(math)</span><br><span class="line">[<span class="string">'__doc__'</span>, <span class="string">'__file__'</span>, <span class="string">'__loader__'</span>, <span class="string">'__name__'</span>, <span class="string">'__package__'</span>, <span class="string">'__spec__'</span>, <span class="string">'acos'</span>, <span class="string">'acosh'</span>, <span class="string">'asin'</span>, <span class="string">'asinh'</span>, <span class="string">'atan'</span>, <span class="string">'atan2'</span>, <span class="string">'atanh'</span>, <span class="string">'ceil'</span>, <span class="string">'copysign'</span>, <span class="string">'cos'</span>, <span class="string">'cosh'</span>, <span class="string">'degrees'</span>, <span class="string">'e'</span>, <span class="string">'erf'</span>, <span class="string">'erfc'</span>, <span class="string">'exp'</span>, <span class="string">'expm1'</span>, <span class="string">'fabs'</span>, <span class="string">'factorial'</span>, <span class="string">'floor'</span>, <span class="string">'fmod'</span>, <span class="string">'frexp'</span>, <span class="string">'fsum'</span>, <span class="string">'gamma'</span>, <span class="string">'gcd'</span>, <span class="string">'hypot'</span>, <span class="string">'inf'</span>, <span class="string">'isclose'</span>, <span class="string">'isfinite'</span>, <span class="string">'isinf'</span>, <span class="string">'isnan'</span>, <span class="string">'ldexp'</span>, <span class="string">'lgamma'</span>, <span class="string">'log'</span>, <span class="string">'log10'</span>, <span class="string">'log1p'</span>, <span class="string">'log2'</span>, <span class="string">'modf'</span>, <span class="string">'nan'</span>, <span class="string">'pi'</span>, <span class="string">'pow'</span>, <span class="string">'radians'</span>, <span class="string">'sin'</span>, <span class="string">'sinh'</span>, <span class="string">'sqrt'</span>, <span class="string">'tan'</span>, <span class="string">'tanh'</span>, <span class="string">'tau'</span>, <span class="string">'trunc'</span>]</span><br><span class="line">&gt;&gt;&gt;</span><br></pre></td></tr></table></figure></p>
<p>查看 cmath 查看包中的内容<br><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">import</span> cmath</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>dir(cmath)</span><br><span class="line">[<span class="string">'__doc__'</span>, <span class="string">'__file__'</span>, <span class="string">'__loader__'</span>, <span class="string">'__name__'</span>, <span class="string">'__package__'</span>, <span class="string">'__spec__'</span>, <span class="string">'acos'</span>, <span class="string">'acosh'</span>, <span class="string">'asin'</span>, <span class="string">'asinh'</span>, <span class="string">'atan'</span>, <span class="string">'atanh'</span>, <span class="string">'cos'</span>, <span class="string">'cosh'</span>, <span class="string">'e'</span>, <span class="string">'exp'</span>, <span class="string">'inf'</span>, <span class="string">'infj'</span>, <span class="string">'isclose'</span>, <span class="string">'isfinite'</span>, <span class="string">'isinf'</span>, <span class="string">'isnan'</span>, <span class="string">'log'</span>, <span class="string">'log10'</span>, <span class="string">'nan'</span>, <span class="string">'nanj'</span>, <span class="string">'phase'</span>, <span class="string">'pi'</span>, <span class="string">'polar'</span>, <span class="string">'rect'</span>, <span class="string">'sin'</span>, <span class="string">'sinh'</span>, <span class="string">'sqrt'</span>, <span class="string">'tan'</span>, <span class="string">'tanh'</span>, <span class="string">'tau'</span>]</span><br><span class="line">&gt;&gt;&gt;</span><br></pre></td></tr></table></figure></p>
<h5 id="Python随机数函数"><a href="#Python随机数函数" class="headerlink" title="Python随机数函数"></a>Python随机数函数</h5><p>随机数可以用于数学，游戏，安全等领域中，还经常被嵌入到算法中，用以提高算法效率，并提高程序的安全性。</p>
<p>Python包含以下常用随机数函数：</p>
<table>
<thead>
<tr>
<th style="text-align:left">函数</th>
<th style="text-align:left">描述</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left"><a href="http://www.runoob.com/python/func-number-choice.html" target="_blank" rel="noopener">choice(seq)</a></td>
<td style="text-align:left">从序列的元素中随机挑选一个元素，比如random.choice(range(10))，从0到9中随机挑选一个整数。</td>
</tr>
<tr>
<td style="text-align:left"><a href="http://www.runoob.com/python/func-number-randrange.html" target="_blank" rel="noopener">randrange ([start,] stop [,step])</a></td>
<td style="text-align:left">从指定范围内，按指定基数递增的集合中获取一个随机数，基数缺省值为1</td>
</tr>
<tr>
<td style="text-align:left"><a href="http://www.runoob.com/python/func-number-random.html" target="_blank" rel="noopener">random()</a></td>
<td style="text-align:left">随机生成下一个实数，它在[0,1)范围内。</td>
</tr>
<tr>
<td style="text-align:left"><a href="http://www.runoob.com/python/func-number-seed.html" target="_blank" rel="noopener">seed([x])</a></td>
<td style="text-align:left">改变随机数生成器的种子seed。如果你不了解其原理，你不必特别去设定seed，Python会帮你选择seed。</td>
</tr>
<tr>
<td style="text-align:left"><a href="http://www.runoob.com/python/func-number-shuffle.html" target="_blank" rel="noopener">shuffle(lst)</a></td>
<td style="text-align:left">将序列的所有元素随机排序</td>
</tr>
<tr>
<td style="text-align:left"><a href="http://www.runoob.com/python/func-number-uniform.html" target="_blank" rel="noopener">uniform(x, y)</a></td>
<td style="text-align:left">随机生成下一个实数，它在[x,y]范围内。</td>
</tr>
</tbody>
</table>
<h5 id="Python三角函数"><a href="#Python三角函数" class="headerlink" title="Python三角函数"></a>Python三角函数</h5><p>Python包括以下三角函数：</p>
<table>
<thead>
<tr>
<th style="text-align:left">函数</th>
<th style="text-align:left">描述</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left"><a href="http://www.runoob.com/python/func-number-acos.html" target="_blank" rel="noopener">acos(x)</a></td>
<td style="text-align:left">返回x的反余弦弧度值。</td>
</tr>
<tr>
<td style="text-align:left"><a href="http://www.runoob.com/python/func-number-asin.html" target="_blank" rel="noopener">asin(x)</a></td>
<td style="text-align:left">返回x的反正弦弧度值。</td>
</tr>
<tr>
<td style="text-align:left"><a href="http://www.runoob.com/python/func-number-atan.html" target="_blank" rel="noopener">atan(x)</a></td>
<td style="text-align:left">返回x的反正切弧度值。</td>
</tr>
<tr>
<td style="text-align:left"><a href="http://www.runoob.com/python/func-number-atan2.html" target="_blank" rel="noopener">atan2(y, x)</a></td>
<td style="text-align:left">返回给定的 X 及 Y 坐标值的反正切值。</td>
</tr>
<tr>
<td style="text-align:left"><a href="http://www.runoob.com/python/func-number-cos.html" target="_blank" rel="noopener">cos(x)</a></td>
<td style="text-align:left">返回x的弧度的余弦值。</td>
</tr>
<tr>
<td style="text-align:left"><a href="http://www.runoob.com/python/func-number-hypot.html" target="_blank" rel="noopener">hypot(x, y)</a></td>
<td style="text-align:left">返回欧几里德范数 sqrt(x<em>x + y</em>y)。</td>
</tr>
<tr>
<td style="text-align:left"><a href="http://www.runoob.com/python/func-number-sin.html" target="_blank" rel="noopener">sin(x)</a></td>
<td style="text-align:left">返回的x弧度的正弦值。</td>
</tr>
<tr>
<td style="text-align:left"><a href="http://www.runoob.com/python/func-number-tan.html" target="_blank" rel="noopener">tan(x)</a></td>
<td style="text-align:left">返回x弧度的正切值。</td>
</tr>
<tr>
<td style="text-align:left"><a href="http://www.runoob.com/python/func-number-degrees.html" target="_blank" rel="noopener">degrees(x)</a></td>
<td style="text-align:left">将弧度转换为角度,如degrees(math.pi/2) ， 返回90.0</td>
</tr>
<tr>
<td style="text-align:left"><a href="http://www.runoob.com/python/func-number-radians.html" target="_blank" rel="noopener">radians(x)</a></td>
<td style="text-align:left">将角度转换为弧度</td>
</tr>
</tbody>
</table>
<hr>
<h5 id="Python数学常量"><a href="#Python数学常量" class="headerlink" title="Python数学常量"></a>Python数学常量</h5><table>
<thead>
<tr>
<th style="text-align:left">常量</th>
<th style="text-align:left">描述</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">pi</td>
<td style="text-align:left">数学常量 pi（圆周率，一般以π来表示）</td>
</tr>
<tr>
<td style="text-align:left">e</td>
<td style="text-align:left">数学常量 e，e即自然常数（自然常数）。</td>
</tr>
</tbody>
</table>
<h4 id="时间"><a href="#时间" class="headerlink" title="时间"></a>时间</h4><h5 id="time"><a href="#time" class="headerlink" title="time"></a>time</h5><p>时间间隔是以秒为单位的浮点小数。</p>
<p>每个时间戳都以自从1970年1月1日午夜（历元）经过了多长时间来表示。</p>
<ul>
<li>Python 的 time 模块下有很多函数可以转换常见日期格式。如函数time.time()用于获取当前时间戳, </li>
</ul>
<h5 id="Time-模块"><a href="#Time-模块" class="headerlink" title="Time 模块"></a>Time 模块</h5><p>Time 模块包含了以下内置函数，既有时间处理的，也有转换时间格式的.<br>详情访问：<a href="https://www.runoob.com/python/python-date-time.html" target="_blank" rel="noopener">Python 日期和时间</a></p>
<h6 id="实例-4"><a href="#实例-4" class="headerlink" title="实例"></a>实例</h6><p>以下实例展示了 time() 函数的使用方法：<br><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#!/usr/bin/python</span></span><br><span class="line"><span class="keyword">import</span> time</span><br><span class="line"></span><br><span class="line"><span class="keyword">print</span> <span class="string">"time.time(): %f "</span> %  time.time()</span><br><span class="line"><span class="keyword">print</span> time.localtime( time.time() )</span><br><span class="line"><span class="keyword">print</span> time.asctime( time.localtime(time.time()) )</span><br></pre></td></tr></table></figure></p>
<p>运行结果：</p>
<blockquote>
<p>1553931680.6086578<br>time.struct_time(tm_year=2019, tm_mon=3, tm_mday=30, tm_hour=15, tm_min=41, tm_sec=57, tm_wday=5, tm_yday=89, tm_isdst=0)<br>Sat Mar 30 15:43:23 2019</p>
</blockquote>
<h5 id="日历（Calendar）模块"><a href="#日历（Calendar）模块" class="headerlink" title="日历（Calendar）模块"></a>日历（Calendar）模块</h5><p>此模块的函数都是日历相关的，例如打印某月的字符月历。</p>
<p>星期一是默认的每周第一天，星期天是默认的最后一天。</p>
<h6 id="实例-5"><a href="#实例-5" class="headerlink" title="实例"></a>实例</h6><p>获取某月日历<br>Calendar模块有很广泛的方法用来处理年历和月历，例如打印某月的月历：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># -*- coding: UTF-8 -*-</span></span><br><span class="line"> </span><br><span class="line"><span class="keyword">import</span> calendar</span><br><span class="line"> </span><br><span class="line">cal = calendar.month(<span class="number">2019</span>, <span class="number">3</span>)</span><br><span class="line"><span class="keyword">print</span> <span class="string">"以下输出2019年3月份的日历:"</span></span><br><span class="line"><span class="keyword">print</span> cal</span><br></pre></td></tr></table></figure>
<p>运行结果：</p>
<blockquote>
<pre><code>March 2019
</code></pre><p>Mo Tu We Th Fr Sa Su<br>             1  2  3<br> 4  5  6  7  8  9 10<br>11 12 13 14 15 16 17<br>18 19 20 21 22 23 24<br>25 26 27 28 29 30 31</p>
</blockquote>
<h4 id="函数"><a href="#函数" class="headerlink" title="函数"></a>函数</h4><h5 id="定义一个函数"><a href="#定义一个函数" class="headerlink" title="定义一个函数"></a>定义一个函数</h5><p>你可以定义一个由自己想要功能的函数，以下是简单的规则：</p>
<ul>
<li>函数代码块以 def 关键词开头，后接函数标识符名称和圆括号()。</li>
<li>任何传入参数和自变量必须放在圆括号中间。圆括号之间可以用于定义参数。</li>
<li>函数的第一行语句可以选择性地使用文档字符串—用于存放函数说明。</li>
<li>函数内容以冒号起始，并且缩进。</li>
<li>return [表达式] 结束函数，选择性地返回一个值给调用方。不带表达式的return相当于返回 None。</li>
</ul>
<h5 id="可更改-mutable-与不可更改-immutable-对象"><a href="#可更改-mutable-与不可更改-immutable-对象" class="headerlink" title="可更改(mutable)与不可更改(immutable)对象"></a>可更改(mutable)与不可更改(immutable)对象</h5><p>在 python 中，strings, tuples, 和 numbers 是不可更改的对象，而 list,dict 等则是可以修改的对象。</p>
<ul>
<li><p>不可变类型：变量赋值 a=5 后再赋值 a=10，这里实际是新生成一个 int 值对象 10，再让 a 指向它，而 5 被丢弃，不是改变a的值，相当于新生成了a。</p>
</li>
<li><p>可变类型：变量赋值 la=[1,2,3,4] 后再赋值 la[2]=5 则是将 list la 的第三个元素值更改，本身la没有动，只是其内部的一部分值被修改了。</p>
</li>
</ul>
<p>python 函数的参数传递：</p>
<ul>
<li><p>不可变类型：类似 c++ 的值传递，如 整数、字符串、元组。如fun（a），传递的只是a的值，没有影响a对象本身。比如在 fun（a）内部修改 a 的值，只是修改另一个复制的对象，不会影响 a 本身。</p>
</li>
<li><p>可变类型：类似 c++ 的引用传递，如 列表，字典。如 fun（la），则是将 la 真正的传过去，修改后fun外部的la也会受影响</p>
</li>
</ul>
<h6 id="实例-6"><a href="#实例-6" class="headerlink" title="实例"></a>实例</h6><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># -*- coding: UTF-8 -*-</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">ChangeInt</span><span class="params">( a )</span>:</span></span><br><span class="line">    a = <span class="number">10</span></span><br><span class="line"> </span><br><span class="line">b = <span class="number">2</span></span><br><span class="line">ChangeInt(b)</span><br><span class="line"><span class="keyword">print</span> b <span class="comment"># 结果是 2</span></span><br></pre></td></tr></table></figure>
<h5 id="参数"><a href="#参数" class="headerlink" title="参数"></a>参数</h5><p>以下是调用函数时可使用的正式参数类型：</p>
<ul>
<li>必备参数</li>
<li>关键字参数</li>
<li>默认参数</li>
<li>不定长参数</li>
</ul>
<h6 id="不定长参数"><a href="#不定长参数" class="headerlink" title="不定长参数"></a>不定长参数</h6><p>你可能需要一个函数能处理比当初声明时更多的参数。这些参数叫做不定长参数，声明时不会命名。基本语法如下：<br><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">functionname</span><span class="params">([formal_args,] *var_args_tuple )</span>:</span></span><br><span class="line">   <span class="string">"函数_文档字符串"</span></span><br><span class="line">   function_suite</span><br><span class="line">   <span class="keyword">return</span> [expression]</span><br></pre></td></tr></table></figure></p>
<p>加了星号（*）的变量名会存放所有未命名的变量参数。不定长参数实例如下：</p>
<p>####### 实例<br><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># -*- coding: UTF-8 -*-</span></span><br><span class="line"> </span><br><span class="line"><span class="comment"># 可写函数说明</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">printinfo</span><span class="params">( arg1, *vartuple )</span>:</span></span><br><span class="line">   <span class="string">"打印任何传入的参数"</span></span><br><span class="line">   <span class="keyword">print</span> <span class="string">"输出: "</span></span><br><span class="line">   <span class="keyword">print</span> arg1</span><br><span class="line">   <span class="keyword">for</span> var <span class="keyword">in</span> vartuple:</span><br><span class="line">      <span class="keyword">print</span> var</span><br><span class="line">   <span class="keyword">return</span>;</span><br><span class="line"> </span><br><span class="line"><span class="comment"># 调用printinfo 函数</span></span><br><span class="line">printinfo( <span class="number">10</span> );</span><br><span class="line">printinfo( <span class="number">70</span>, <span class="number">60</span>, <span class="number">50</span> );</span><br></pre></td></tr></table></figure></p>
<p>以上实例输出结果：</p>
<blockquote>
<p>输出:<br>10<br>输出:<br>70<br>60<br>50</p>
</blockquote>
<h5 id="匿名函数"><a href="#匿名函数" class="headerlink" title="匿名函数"></a>匿名函数</h5><p>python 使用 lambda 来创建匿名函数。</p>
<ul>
<li>lambda只是一个表达式，函数体比def简单很多。</li>
<li>lambda的主体是一个表达式，而不是一个代码块。仅仅能在lambda表达式中封装有限的逻辑进去。</li>
<li>lambda函数拥有自己的命名空间，且不能访问自有参数列表之外或全局命名空间里的参数。</li>
<li>虽然lambda函数看起来只能写一行，却不等同于C或C++的内联函数，后者的目的是调用小函数时不占用栈内存从而增加运行效率。<h6 id="语法"><a href="#语法" class="headerlink" title="语法"></a>语法</h6>lambda函数的语法只包含一个语句，如下：</li>
</ul>
<blockquote>
<p>lambda [arg1 [,arg2,…..argn]]:expression</p>
</blockquote>
<h6 id="实例-7"><a href="#实例-7" class="headerlink" title="实例"></a>实例</h6><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># -*- coding: UTF-8 -*-</span></span><br><span class="line"> </span><br><span class="line"><span class="comment"># 可写函数说明</span></span><br><span class="line">sum = <span class="keyword">lambda</span> arg1, arg2: arg1 + arg2;</span><br><span class="line"> </span><br><span class="line"><span class="comment"># 调用sum函数</span></span><br><span class="line"><span class="keyword">print</span> <span class="string">"相加后的值为 : "</span>, sum( <span class="number">10</span>, <span class="number">20</span> )</span><br><span class="line"><span class="keyword">print</span> <span class="string">"相加后的值为 : "</span>, sum( <span class="number">20</span>, <span class="number">20</span> )</span><br></pre></td></tr></table></figure>
<p>以上实例输出结果：</p>
<blockquote>
<p>相加后的值为 :  30<br>相加后的值为 :  40</p>
</blockquote>
<h4 id="Python中的包"><a href="#Python中的包" class="headerlink" title="Python中的包"></a>Python中的包</h4><p>包是一个分层次的文件目录结构，它定义了一个由模块及子包，和子包下的子包等组成的 Python 的应用环境。</p>
<p>简单来说，包就是文件夹，但该文件夹下必须存在 <strong>init</strong>.py 文件, 该文件的内容可以为空。<strong>init</strong>.py 用于标识当前文件夹是一个包。<br>例如：<br>考虑一个在 package_runoob 目录下的 runoob1.py、runoob2.py、<strong>init</strong>.py 文件，test.py 为测试调用包的代码，目录结构如下：<br><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">test.py</span><br><span class="line">package_runoob</span><br><span class="line">|-- __init__.py</span><br><span class="line">|-- runoob1.py</span><br><span class="line">|-- runoob2.py 源代码如下：</span><br><span class="line"></span><br><span class="line">package_runoob/runoob1.py</span><br><span class="line"></span><br><span class="line">​```python</span><br><span class="line"><span class="comment"># -*- coding: UTF-8 -*-</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">runoob1</span><span class="params">()</span>:</span></span><br><span class="line">   <span class="keyword">print</span> <span class="string">"I'm in runoob1"</span></span><br></pre></td></tr></table></figure></p>
<p>package_runoob/runoob2.py</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># -*- coding: UTF-8 -*-</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">runoob2</span><span class="params">()</span>:</span></span><br><span class="line">   <span class="keyword">print</span> <span class="string">"I'm in runoob2"</span></span><br></pre></td></tr></table></figure>
<p>现在，在 package_runoob 目录下创建 <strong>init</strong>.py：</p>
<p>package_runoob/<strong>init</strong>.py<br><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="comment"># -*- coding: UTF-8 -*-</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</span><br><span class="line">    <span class="keyword">print</span> <span class="string">'作为主程序运行'</span></span><br><span class="line"><span class="keyword">else</span>:</span><br><span class="line">    <span class="keyword">print</span> <span class="string">'package_runoob 初始化'</span></span><br></pre></td></tr></table></figure></p>
<p>然后我们在 package_runoob 同级目录下创建 test.py 来调用 package_runoob 包</p>
<p>test.py<br><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># -*- coding: UTF-8 -*-</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 导入 Phone 包</span></span><br><span class="line"><span class="keyword">from</span> package_runoob.runoob1 <span class="keyword">import</span> runoob1</span><br><span class="line"><span class="keyword">from</span> package_runoob.runoob2 <span class="keyword">import</span> runoob2</span><br><span class="line"></span><br><span class="line">runoob1()</span><br><span class="line">runoob2()</span><br></pre></td></tr></table></figure></p>
<p>以上实例输出结果：</p>
<blockquote>
<p>package_runoob 初始化<br>I’m in runoob1<br>I’m in runoob2</p>
</blockquote>
<h4 id="文件"><a href="#文件" class="headerlink" title="文件"></a>文件</h4><h5 id="读取键盘输入"><a href="#读取键盘输入" class="headerlink" title="读取键盘输入"></a>读取键盘输入</h5><p>Python提供了两个内置函数从标准输入读入一行文本，默认的标准输入是键盘。如下：</p>
<ul>
<li>raw_input</li>
<li>input<h6 id="raw-input函数"><a href="#raw-input函数" class="headerlink" title="raw_input函数"></a>raw_input函数</h6>raw_input([prompt]) 函数从标准输入读取一个行，并返回一个字符串（去掉结尾的换行符）：</li>
</ul>
<p>####### 实例<br><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># -*- coding: UTF-8 -*- </span></span><br><span class="line"> </span><br><span class="line">str = raw_input(<span class="string">"请输入："</span>)</span><br><span class="line"><span class="keyword">print</span> <span class="string">"你输入的内容是: "</span>, str</span><br></pre></td></tr></table></figure></p>
<p>这会产生如下的对应着输入的结果：</p>
<blockquote>
<p>请输入：Hello Python！<br>你输入的内容是:  Hello Python！</p>
</blockquote>
<h6 id="input函数"><a href="#input函数" class="headerlink" title="input函数"></a>input函数</h6><p>input([prompt]) 函数和 raw_input([prompt]) 函数基本类似，但是 input 可以接收一个Python表达式作为输入，并将运算结果返回。</p>
<p>####### 实例<br><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># -*- coding: UTF-8 -*- </span></span><br><span class="line"> </span><br><span class="line">str = input(<span class="string">"请输入："</span>)</span><br><span class="line"><span class="keyword">print</span> <span class="string">"你输入的内容是: "</span>, str</span><br></pre></td></tr></table></figure></p>
<p>这会产生如下的对应着输入的结果：</p>
<blockquote>
<p>请输入：[x*5 for x in range(2,10,2)]<br>你输入的内容是:  [10, 20, 30, 40]</p>
</blockquote>
<h5 id="打开和关闭文件"><a href="#打开和关闭文件" class="headerlink" title="打开和关闭文件"></a>打开和关闭文件</h5><p>Python 提供了必要的函数和方法进行默认情况下的文件基本操作。你可以用 <strong>file</strong> 对象做大部分的文件操作。</p>
<h6 id="open-函数"><a href="#open-函数" class="headerlink" title="open 函数"></a>open 函数</h6><p>你必须先用Python内置的open()函数打开一个文件，创建一个file对象，相关的方法才可以调用它进行读写。</p>
<p>语法：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">file object = open(file_name [, access_mode][, buffering])</span><br></pre></td></tr></table></figure>
<p>各个参数的细节如下：</p>
<ul>
<li>file_name：file_name变量是一个包含了你要访问的文件名称的字符串值。</li>
<li>access_mode：access_mode决定了打开文件的模式：只读，写入，追加等。所有可取值见如下的完全列表。这个参数是非强制的，默认文件访问模式为只读(r)。</li>
<li>buffering:如果buffering的值被设为0，就不会有寄存。如果buffering的值取1，访问文件时会寄存行。如果将buffering的值设为大于1的整数，表明了这就是的寄存区的缓冲大小。如果取负值，寄存区的缓冲大小则为系统默认。</li>
</ul>
<p><strong>注意：</strong>使用 open() 方法一定要保证关闭文件对象，即调用 close() 方法。</p>
<p>open() 函数常用形式是接收两个参数：文件名(file)和模式(mode)。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">open(file, mode=&apos;r&apos;)</span><br></pre></td></tr></table></figure>
<p>完整的语法格式为：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">open(file, mode=&apos;r&apos;, buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)</span><br></pre></td></tr></table></figure>
<p>参数说明:</p>
<ul>
<li>file: 必需，文件路径（相对或者绝对路径）。</li>
<li>mode: 可选，文件打开模式</li>
<li>buffering: 设置缓冲</li>
<li>encoding: 一般使用utf8</li>
<li>errors: 报错级别</li>
<li>newline: 区分换行符</li>
<li>closefd: 传入的file参数类型</li>
<li>opener:</li>
</ul>
<p>不同模式打开文件的完全列表：</p>
<table>
<thead>
<tr>
<th style="text-align:left">模式</th>
<th style="text-align:left">描述</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">t</td>
<td style="text-align:left">文本模式 (默认)。</td>
</tr>
<tr>
<td style="text-align:left">x</td>
<td style="text-align:left">写模式，新建一个文件，如果该文件已存在则会报错。</td>
</tr>
<tr>
<td style="text-align:left">b</td>
<td style="text-align:left">二进制模式。</td>
</tr>
<tr>
<td style="text-align:left">+</td>
<td style="text-align:left">打开一个文件进行更新(可读可写)。</td>
</tr>
<tr>
<td style="text-align:left">U</td>
<td style="text-align:left">通用换行模式（不推荐）。</td>
</tr>
<tr>
<td style="text-align:left">r</td>
<td style="text-align:left">以只读方式打开文件。文件的指针将会放在文件的开头。这是默认模式。</td>
</tr>
<tr>
<td style="text-align:left">rb</td>
<td style="text-align:left">以二进制格式打开一个文件用于只读。文件指针将会放在文件的开头。这是默认模式。一般用于非文本文件如图片等。</td>
</tr>
<tr>
<td style="text-align:left">r+</td>
<td style="text-align:left">打开一个文件用于读写。文件指针将会放在文件的开头。</td>
</tr>
<tr>
<td style="text-align:left">rb+</td>
<td style="text-align:left">以二进制格式打开一个文件用于读写。文件指针将会放在文件的开头。一般用于非文本文件如图片等。</td>
</tr>
<tr>
<td style="text-align:left">w</td>
<td style="text-align:left">打开一个文件只用于写入。如果该文件已存在则打开文件，并从开头开始编辑，即原有内容会被删除。如果该文件不存在，创建新文件。</td>
</tr>
<tr>
<td style="text-align:left">wb</td>
<td style="text-align:left">以二进制格式打开一个文件只用于写入。如果该文件已存在则打开文件，并从开头开始编辑，即原有内容会被删除。如果该文件不存在，创建新文件。一般用于非文本文件如图片等。</td>
</tr>
<tr>
<td style="text-align:left">w+</td>
<td style="text-align:left">打开一个文件用于读写。如果该文件已存在则打开文件，并从开头开始编辑，即原有内容会被删除。如果该文件不存在，创建新文件。</td>
</tr>
<tr>
<td style="text-align:left">wb+</td>
<td style="text-align:left">以二进制格式打开一个文件用于读写。如果该文件已存在则打开文件，并从开头开始编辑，即原有内容会被删除。如果该文件不存在，创建新文件。一般用于非文本文件如图片等。</td>
</tr>
<tr>
<td style="text-align:left">a</td>
<td style="text-align:left">打开一个文件用于追加。如果该文件已存在，文件指针将会放在文件的结尾。也就是说，新的内容将会被写入到已有内容之后。如果该文件不存在，创建新文件进行写入。</td>
</tr>
<tr>
<td style="text-align:left">ab</td>
<td style="text-align:left">以二进制格式打开一个文件用于追加。如果该文件已存在，文件指针将会放在文件的结尾。也就是说，新的内容将会被写入到已有内容之后。如果该文件不存在，创建新文件进行写入。</td>
</tr>
<tr>
<td style="text-align:left">a+</td>
<td style="text-align:left">打开一个文件用于读写。如果该文件已存在，文件指针将会放在文件的结尾。文件打开时会是追加模式。如果该文件不存在，创建新文件用于读写。</td>
</tr>
<tr>
<td style="text-align:left">ab+</td>
<td style="text-align:left">以二进制格式打开一个文件用于追加。如果该文件已存在，文件指针将会放在文件的结尾。如果该文件不存在，创建新文件用于读写。</td>
</tr>
</tbody>
</table>
<h6 id="File对象的属性"><a href="#File对象的属性" class="headerlink" title="File对象的属性"></a>File对象的属性</h6><p>一个文件被打开后，你有一个file对象，你可以得到有关该文件的各种信息。</p>
<p>以下是和file对象相关的所有属性的列表：</p>
<table>
<thead>
<tr>
<th style="text-align:left">属性</th>
<th style="text-align:left">描述</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">file.closed</td>
<td style="text-align:left">返回true如果文件已被关闭，否则返回false。</td>
</tr>
<tr>
<td style="text-align:left">file.mode</td>
<td style="text-align:left">返回被打开文件的访问模式。</td>
</tr>
<tr>
<td style="text-align:left">file.name</td>
<td style="text-align:left">返回文件的名称。</td>
</tr>
<tr>
<td style="text-align:left">file.softspace</td>
<td style="text-align:left">如果用print输出后，必须跟一个空格符，则返回false。否则返回true。</td>
</tr>
</tbody>
</table>
<p>如下实例：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># -*- coding: UTF-8 -*-</span></span><br><span class="line"> </span><br><span class="line"><span class="comment"># 打开一个文件</span></span><br><span class="line">fo = open(<span class="string">"foo.txt"</span>, <span class="string">"w"</span>)</span><br><span class="line"><span class="keyword">print</span> <span class="string">"文件名: "</span>, fo.name</span><br><span class="line"><span class="keyword">print</span> <span class="string">"是否已关闭 : "</span>, fo.closed</span><br><span class="line"><span class="keyword">print</span> <span class="string">"访问模式 : "</span>, fo.mode</span><br><span class="line"><span class="keyword">print</span> <span class="string">"末尾是否强制加空格 : "</span>, fo.softspace</span><br></pre></td></tr></table></figure>
<p>以上实例输出结果：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">文件名:  foo.txt</span><br><span class="line">是否已关闭 :  False</span><br><span class="line">访问模式 :  w</span><br><span class="line">末尾是否强制加空格 :  0</span><br></pre></td></tr></table></figure>
<h6 id="write-方法"><a href="#write-方法" class="headerlink" title="write()方法"></a>write()方法</h6><p>write()方法可将任何字符串写入一个打开的文件。需要重点注意的是，Python字符串可以是二进制数据，而不是仅仅是文字。</p>
<p>write()方法不会在字符串的结尾添加换行符(‘\n’)：</p>
<h6 id="read-方法"><a href="#read-方法" class="headerlink" title="read()方法"></a>read()方法</h6><p>read（）方法从一个打开的文件中读取一个字符串。需要重点注意的是，Python字符串可以是二进制数据，而不是仅仅是文字。<br>语法：</p>
<blockquote>
<p>fileObject.read([count])</p>
</blockquote>
<p>在这里，被传递的参数是要从已打开文件中读取的字节计数。该方法从文件的开头开始读入，如果没有传入count，它会尝试尽可能多地读取更多的内容，很可能是直到文件的末尾。</p>
<h6 id="文件定位"><a href="#文件定位" class="headerlink" title="文件定位"></a>文件定位</h6><ul>
<li><p>tell()方法告诉你文件内的当前位置, 换句话说，下一次的读写会发生在文件开头这么多字节之后。</p>
</li>
<li><p>seek（offset [,from]）方法改变当前文件的位置。Offset变量表示要移动的字节数。From变量指定开始移动字节的<strong>参考位置</strong>。如果from被设为0，这意味着将文件的开头作为移动字节的参考位置。如果设为1，则使用当前的位置作为参考位置。如果它被设为2，那么该文件的末尾将作为参考位置。</p>
</li>
</ul>
<h6 id="重命名和删除文件"><a href="#重命名和删除文件" class="headerlink" title="重命名和删除文件"></a>重命名和删除文件</h6><p>Python的os模块提供了帮你执行文件处理操作的方法，比如重命名和删除文件。</p>
<p>要使用这个模块，你必须先导入它，然后才可以调用相关的各种功能。</p>
<p>####### rename()方法：</p>
<p>rename()方法需要两个参数，当前的文件名和新文件名。</p>
<p>语法：</p>
<blockquote>
<p>os.rename(current_file_name, new_file_name)</p>
</blockquote>
<p>######## 实例<br>下例将重命名一个已经存在的文件test1.txt。<br><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># -*- coding: UTF-8 -*-</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> os</span><br><span class="line"> </span><br><span class="line"><span class="comment"># 重命名文件test1.txt到test2.txt。</span></span><br><span class="line">os.rename( <span class="string">"test1.txt"</span>, <span class="string">"test2.txt"</span> )</span><br></pre></td></tr></table></figure></p>
<p>####### remove()方法<br>你可以用remove()方法删除文件，需要提供要删除的文件名作为参数。</p>
<p>语法：</p>
<blockquote>
<p>os.remove(file_name)</p>
</blockquote>
<p>######## 实例<br>下例将删除一个已经存在的文件test2.txt。<br><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># -*- coding: UTF-8 -*-</span></span><br><span class="line"><span class="keyword">import</span> os</span><br><span class="line"></span><br><span class="line"><span class="comment"># 删除一个已经存在的文件test2.txt</span></span><br><span class="line">os.remove(<span class="string">"test2.txt"</span>)</span><br></pre></td></tr></table></figure></p>
<h5 id="Python里的目录："><a href="#Python里的目录：" class="headerlink" title="Python里的目录："></a>Python里的目录：</h5><p>所有文件都包含在各个不同的目录下，不过Python也能轻松处理。os模块有许多方法能帮你创建，删除和更改目录。<br><a href="http://www.runoob.com/python/os-file-methods.html" target="_blank" rel="noopener">目录方法</a></p>
<h6 id="mkdir-方法"><a href="#mkdir-方法" class="headerlink" title="mkdir()方法"></a>mkdir()方法</h6><p>可以使用os模块的mkdir()方法在当前目录下创建新的目录们。你需要提供一个包含了要创建的目录名称的参数。</p>
<p>语法：</p>
<blockquote>
<p>os.mkdir(“newdir”)<br>例子：</p>
</blockquote>
<h6 id="chdir-方法"><a href="#chdir-方法" class="headerlink" title="chdir()方法"></a>chdir()方法</h6><p>可以用chdir()方法来改变当前的目录。chdir()方法需要的一个参数是你想设成当前目录的目录名称。</p>
<p>语法：</p>
<blockquote>
<p>os.chdir(“newdir”)</p>
</blockquote>
<h6 id="getcwd-方法："><a href="#getcwd-方法：" class="headerlink" title="getcwd()方法："></a>getcwd()方法：</h6><p>getcwd()方法显示当前的工作目录。</p>
<p>语法：</p>
<blockquote>
<p>os.getcwd()</p>
</blockquote>
<h6 id="rmdir-方法"><a href="#rmdir-方法" class="headerlink" title="rmdir()方法"></a>rmdir()方法</h6><p>rmdir()方法删除目录，目录名称以参数传递。</p>
<p>在删除这个目录之前，它的所有内容应该先被清除。</p>
<p>语法：</p>
<blockquote>
<p>os.rmdir(‘dirname’)</p>
</blockquote>
<h4 id="异常处理"><a href="#异常处理" class="headerlink" title="异常处理"></a>异常处理</h4><p><a href="http://www.runoob.com/python/python-exceptions.html" target="_blank" rel="noopener">异常处理</a></p>
<h5 id="try-except语句。"><a href="#try-except语句。" class="headerlink" title="try/except语句。"></a>try/except语句。</h5><p>try/except语句用来检测try语句块中的错误，从而让except语句捕获异常信息并处理。</p>
<p>如果你不想在异常发生时结束你的程序，只需在try里捕获它。</p>
<p>语法：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">try:</span><br><span class="line">&lt;语句&gt;        #运行别的代码</span><br><span class="line">except &lt;名字&gt;：</span><br><span class="line">&lt;语句&gt;        #如果在try部份引发了&apos;name&apos;异常</span><br><span class="line">except &lt;名字&gt;，&lt;数据&gt;:</span><br><span class="line">&lt;语句&gt;        #如果引发了&apos;name&apos;异常，获得附加的数据</span><br><span class="line">else:</span><br><span class="line">&lt;语句&gt;        #如果没有异常发生</span><br></pre></td></tr></table></figure>
<h5 id="try-finally-语句"><a href="#try-finally-语句" class="headerlink" title="try-finally 语句"></a>try-finally 语句</h5><p>try-finally 语句无论是否发生异常都将执行最后的代码。<br>语法</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">try:</span><br><span class="line">&lt;语句&gt;</span><br><span class="line">finally:</span><br><span class="line">&lt;语句&gt;    #退出try时总会执行</span><br><span class="line">raise</span><br></pre></td></tr></table></figure>
<h6 id="实例-8"><a href="#实例-8" class="headerlink" title="实例"></a>实例</h6><p>以下为单个异常的实例：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># -*- coding: UTF-8 -*-</span></span><br><span class="line"><span class="comment"># 定义函数</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">temp_convert</span><span class="params">(var)</span>:</span></span><br><span class="line">    <span class="keyword">try</span>:</span><br><span class="line">        <span class="keyword">return</span> int(var)</span><br><span class="line">    <span class="keyword">except</span> ValueError, Argument:</span><br><span class="line">        <span class="keyword">print</span> <span class="string">"参数没有包含数字\n"</span>, Argument</span><br><span class="line"></span><br><span class="line"><span class="comment"># 调用函数</span></span><br><span class="line">temp_convert(<span class="string">"xyz"</span>);</span><br></pre></td></tr></table></figure>
<p>以上程序执行结果如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">$ python test.py </span><br><span class="line">参数没有包含数字</span><br><span class="line">invalid literal for int() with base 10: &apos;xyz&apos;</span><br></pre></td></tr></table></figure>
<h5 id="触发异常"><a href="#触发异常" class="headerlink" title="触发异常"></a>触发异常</h5><p>我们可以使用raise语句自己触发异常</p>
<p>raise语法格式如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">raise [Exception [, args [, traceback]]]</span><br></pre></td></tr></table></figure>
<p>语句中 Exception 是异常的类型（例如，NameError）参数标准异常中任一种，args 是自已提供的异常参数。</p>
<p>最后一个参数是可选的（在实践中很少使用），如果存在，是跟踪异常对象。</p>
<h6 id="实例-9"><a href="#实例-9" class="headerlink" title="实例"></a>实例</h6><p>一个异常可以是一个字符串，类或对象。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">functionName</span><span class="params">( level )</span>:</span></span><br><span class="line">    <span class="keyword">if</span> level &lt; <span class="number">1</span>:</span><br><span class="line">        <span class="keyword">raise</span> Exception(<span class="string">"Invalid level!"</span>, level)</span><br><span class="line">        <span class="comment"># 触发异常后，后面的代码就不会再执行</span></span><br></pre></td></tr></table></figure>
<h4 id="面向对象"><a href="#面向对象" class="headerlink" title="面向对象"></a>面向对象</h4><ul>
<li>getattr(obj, name[, default]) : 访问对象的属性。</li>
<li>hasattr(obj,name) : 检查是否存在一个属性。</li>
<li>setattr(obj,name,value) : 设置一个属性。如果属性不存在，会创建一个新属性。</li>
<li>delattr(obj, name) : 删除属性。</li>
</ul>
<ul>
<li>issubclass() - 布尔函数判断一个类是另一个类的子类或者子孙类，语法：issubclass(sub,sup)</li>
<li>isinstance(obj, Class) 布尔函数如果obj是Class类的实例对象或者是一个Class子类的实例对象则返回true。</li>
</ul>
<h5 id="基础重载方法"><a href="#基础重载方法" class="headerlink" title="基础重载方法"></a>基础重载方法</h5><table>
<thead>
<tr>
<th style="text-align:left">序号</th>
<th style="text-align:left">方法, 描述 &amp; 简单的调用</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">1</td>
<td style="text-align:left"><strong><strong>init</strong> ( self [,args…] )</strong> 构造函数 简单的调用方法: <em>obj = className(args)</em></td>
</tr>
<tr>
<td style="text-align:left">2</td>
<td style="text-align:left"><strong><strong>del</strong>( self )</strong> 析构方法, 删除一个对象 简单的调用方法 : <em>del obj</em></td>
</tr>
<tr>
<td style="text-align:left">3</td>
<td style="text-align:left"><strong><strong>repr</strong>( self )</strong> 转化为供解释器读取的形式 简单的调用方法 : <em>repr(obj)</em></td>
</tr>
<tr>
<td style="text-align:left">4</td>
<td style="text-align:left"><strong><strong>str</strong>( self )</strong> 用于将值转化为适于人阅读的形式 简单的调用方法 : <em>str(obj)</em></td>
</tr>
<tr>
<td style="text-align:left">5</td>
<td style="text-align:left"><strong><strong>cmp</strong> ( self, x )</strong> 对象比较 简单的调用方法 : <em>cmp(obj, x)</em></td>
</tr>
</tbody>
</table>

      
    </div>
    
    
    

    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>你可以对我进行打赏哦</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="/images/weichat.jpg" alt="ananzhusen 微信支付">
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="/images/alipay.jpg" alt="ananzhusen 支付宝">
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/python基础/" rel="tag"># python基础</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2019/03/23/第一篇博客/" rel="next" title="第一篇博客：在Github上搭建Hexo博客">
                <i class="fa fa-chevron-left"></i> 第一篇博客：在Github上搭建Hexo博客
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2019/04/11/学习Django框架/" rel="prev" title="学习Django框架">
                学习Django框架 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
        <!-- JiaThis Button BEGIN -->
<div class="jiathis_style">
<span class="jiathis_txt">分享到：</span>
<a class="jiathis_button_fav">收藏夹</a>
<a class="jiathis_button_copy">复制网址</a>
<a class="jiathis_button_email">邮件</a>
<a class="jiathis_button_weixin">微信</a>
<a class="jiathis_button_qzone">QQ空间</a>
<a class="jiathis_button_tqq">腾讯微博</a>
<a class="jiathis_button_douban">豆瓣</a>
<a class="jiathis_button_share">一键分享</a>

<a href="http://www.jiathis.com/share?uid=2140465" class="jiathis jiathis_txt jiathis_separator jtico jtico_jiathis" target="_blank">更多</a>
<a class="jiathis_counter_style"></a>
</div>
<script type="text/javascript">
var jiathis_config={
  data_track_clickback:true,
  summary:"",
  shortUrl:false,
  hideMore:false
}
</script>
<script type="text/javascript" src="http://v3.jiathis.com/code/jia.js?uid=" charset="utf-8"></script>
<!-- JiaThis Button END -->
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
    </div>
  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/images/touxiang.JPG" alt="ananzhusen">
            
              <p class="site-author-name" itemprop="name">ananzhusen</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">23</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">13</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">43</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/ananzhusen" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://weibo.com/5817162963" target="_blank" title="weibo">
                      
                        <i class="fa fa-fw fa-weibo"></i>weibo</a>
                  </span>
                
            </div>
          

          
          

          
          
            <div class="links-of-blogroll motion-element links-of-blogroll-block">
              <div class="links-of-blogroll-title">
                <i class="fa  fa-fw fa-link"></i>
                Links
              </div>
              <ul class="links-of-blogroll-list">
                
                  <li class="links-of-blogroll-item">
                    <a href="https://gitee.com/anzhusen/" title="gitee" target="_blank">gitee</a>
                  </li>
                
              </ul>
            </div>
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#巩固python基础"><span class="nav-number">1.</span> <span class="nav-text">巩固python基础</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#中文编码"><span class="nav-number">1.1.</span> <span class="nav-text">中文编码</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#标识符"><span class="nav-number">1.2.</span> <span class="nav-text">标识符</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#行和缩进"><span class="nav-number">1.3.</span> <span class="nav-text">行和缩进</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#多行语句"><span class="nav-number">1.4.</span> <span class="nav-text">多行语句</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Print-输出"><span class="nav-number">1.5.</span> <span class="nav-text">Print 输出</span></a></li></ol></li></ol><li class="nav-item nav-level-2"><a class="nav-link" href="#b"><span class="nav-number"></span> <span class="nav-text">b</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#标准数据类型"><span class="nav-number">0.1.</span> <span class="nav-text">标准数据类型</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#数字"><span class="nav-number">0.1.1.</span> <span class="nav-text">数字</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#字符串"><span class="nav-number">0.1.2.</span> <span class="nav-text">字符串</span></a><ol class="nav-child"><li class="nav-item nav-level-6"><a class="nav-link" href="#实例"><span class="nav-number">0.1.2.1.</span> <span class="nav-text">实例</span></a></li></ol></li><li class="nav-item nav-level-5"><a class="nav-link" href="#列表"><span class="nav-number">0.1.3.</span> <span class="nav-text">列表</span></a><ol class="nav-child"><li class="nav-item nav-level-6"><a class="nav-link" href="#列表方法"><span class="nav-number">0.1.3.1.</span> <span class="nav-text">列表方法</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#实例-1"><span class="nav-number">0.1.3.2.</span> <span class="nav-text">实例</span></a></li></ol></li><li class="nav-item nav-level-5"><a class="nav-link" href="#元组"><span class="nav-number">0.1.4.</span> <span class="nav-text">元组</span></a><ol class="nav-child"><li class="nav-item nav-level-6"><a class="nav-link" href="#实例-2"><span class="nav-number">0.1.4.1.</span> <span class="nav-text">实例</span></a></li></ol></li><li class="nav-item nav-level-5"><a class="nav-link" href="#字典"><span class="nav-number">0.1.5.</span> <span class="nav-text">字典</span></a><ol class="nav-child"><li class="nav-item nav-level-6"><a class="nav-link" href="#删除字典元素"><span class="nav-number">0.1.5.1.</span> <span class="nav-text">删除字典元素</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#内置方法："><span class="nav-number">0.1.5.2.</span> <span class="nav-text">内置方法：</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#实例-3"><span class="nav-number">0.1.5.3.</span> <span class="nav-text">实例</span></a></li></ol></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#运算符"><span class="nav-number">0.2.</span> <span class="nav-text">运算符</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#算数运算符"><span class="nav-number">0.2.1.</span> <span class="nav-text">算数运算符</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#位运算符"><span class="nav-number">0.2.2.</span> <span class="nav-text">位运算符</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#逻辑运算符"><span class="nav-number">0.2.3.</span> <span class="nav-text">逻辑运算符</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#成员运算符"><span class="nav-number">0.2.4.</span> <span class="nav-text">成员运算符</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#身份运算符"><span class="nav-number">0.2.5.</span> <span class="nav-text">身份运算符</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#字符串运算符"><span class="nav-number">0.2.6.</span> <span class="nav-text">字符串运算符</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#运算符优先级"><span class="nav-number">0.2.7.</span> <span class="nav-text">运算符优先级</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#语句"><span class="nav-number">0.3.</span> <span class="nav-text">语句</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#pass-语句"><span class="nav-number">0.3.1.</span> <span class="nav-text">pass 语句</span></a><ol class="nav-child"><li class="nav-item nav-level-6"><a class="nav-link" href="#实例："><span class="nav-number">0.3.1.1.</span> <span class="nav-text">实例：</span></a></li></ol></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#数学函数"><span class="nav-number">0.4.</span> <span class="nav-text">数学函数</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#math-模块、cmath-模块"><span class="nav-number">0.4.1.</span> <span class="nav-text">math 模块、cmath 模块</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#Python随机数函数"><span class="nav-number">0.4.2.</span> <span class="nav-text">Python随机数函数</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#Python三角函数"><span class="nav-number">0.4.3.</span> <span class="nav-text">Python三角函数</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#Python数学常量"><span class="nav-number">0.4.4.</span> <span class="nav-text">Python数学常量</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#时间"><span class="nav-number">0.5.</span> <span class="nav-text">时间</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#time"><span class="nav-number">0.5.1.</span> <span class="nav-text">time</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#Time-模块"><span class="nav-number">0.5.2.</span> <span class="nav-text">Time 模块</span></a><ol class="nav-child"><li class="nav-item nav-level-6"><a class="nav-link" href="#实例-4"><span class="nav-number">0.5.2.1.</span> <span class="nav-text">实例</span></a></li></ol></li><li class="nav-item nav-level-5"><a class="nav-link" href="#日历（Calendar）模块"><span class="nav-number">0.5.3.</span> <span class="nav-text">日历（Calendar）模块</span></a><ol class="nav-child"><li class="nav-item nav-level-6"><a class="nav-link" href="#实例-5"><span class="nav-number">0.5.3.1.</span> <span class="nav-text">实例</span></a></li></ol></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#函数"><span class="nav-number">0.6.</span> <span class="nav-text">函数</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#定义一个函数"><span class="nav-number">0.6.1.</span> <span class="nav-text">定义一个函数</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#可更改-mutable-与不可更改-immutable-对象"><span class="nav-number">0.6.2.</span> <span class="nav-text">可更改(mutable)与不可更改(immutable)对象</span></a><ol class="nav-child"><li class="nav-item nav-level-6"><a class="nav-link" href="#实例-6"><span class="nav-number">0.6.2.1.</span> <span class="nav-text">实例</span></a></li></ol></li><li class="nav-item nav-level-5"><a class="nav-link" href="#参数"><span class="nav-number">0.6.3.</span> <span class="nav-text">参数</span></a><ol class="nav-child"><li class="nav-item nav-level-6"><a class="nav-link" href="#不定长参数"><span class="nav-number">0.6.3.1.</span> <span class="nav-text">不定长参数</span></a></li></ol></li><li class="nav-item nav-level-5"><a class="nav-link" href="#匿名函数"><span class="nav-number">0.6.4.</span> <span class="nav-text">匿名函数</span></a><ol class="nav-child"><li class="nav-item nav-level-6"><a class="nav-link" href="#语法"><span class="nav-number">0.6.4.1.</span> <span class="nav-text">语法</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#实例-7"><span class="nav-number">0.6.4.2.</span> <span class="nav-text">实例</span></a></li></ol></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Python中的包"><span class="nav-number">0.7.</span> <span class="nav-text">Python中的包</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#文件"><span class="nav-number">0.8.</span> <span class="nav-text">文件</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#读取键盘输入"><span class="nav-number">0.8.1.</span> <span class="nav-text">读取键盘输入</span></a><ol class="nav-child"><li class="nav-item nav-level-6"><a class="nav-link" href="#raw-input函数"><span class="nav-number">0.8.1.1.</span> <span class="nav-text">raw_input函数</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#input函数"><span class="nav-number">0.8.1.2.</span> <span class="nav-text">input函数</span></a></li></ol></li><li class="nav-item nav-level-5"><a class="nav-link" href="#打开和关闭文件"><span class="nav-number">0.8.2.</span> <span class="nav-text">打开和关闭文件</span></a><ol class="nav-child"><li class="nav-item nav-level-6"><a class="nav-link" href="#open-函数"><span class="nav-number">0.8.2.1.</span> <span class="nav-text">open 函数</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#File对象的属性"><span class="nav-number">0.8.2.2.</span> <span class="nav-text">File对象的属性</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#write-方法"><span class="nav-number">0.8.2.3.</span> <span class="nav-text">write()方法</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#read-方法"><span class="nav-number">0.8.2.4.</span> <span class="nav-text">read()方法</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#文件定位"><span class="nav-number">0.8.2.5.</span> <span class="nav-text">文件定位</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#重命名和删除文件"><span class="nav-number">0.8.2.6.</span> <span class="nav-text">重命名和删除文件</span></a></li></ol></li><li class="nav-item nav-level-5"><a class="nav-link" href="#Python里的目录："><span class="nav-number">0.8.3.</span> <span class="nav-text">Python里的目录：</span></a><ol class="nav-child"><li class="nav-item nav-level-6"><a class="nav-link" href="#mkdir-方法"><span class="nav-number">0.8.3.1.</span> <span class="nav-text">mkdir()方法</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#chdir-方法"><span class="nav-number">0.8.3.2.</span> <span class="nav-text">chdir()方法</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#getcwd-方法："><span class="nav-number">0.8.3.3.</span> <span class="nav-text">getcwd()方法：</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#rmdir-方法"><span class="nav-number">0.8.3.4.</span> <span class="nav-text">rmdir()方法</span></a></li></ol></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#异常处理"><span class="nav-number">0.9.</span> <span class="nav-text">异常处理</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#try-except语句。"><span class="nav-number">0.9.1.</span> <span class="nav-text">try/except语句。</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#try-finally-语句"><span class="nav-number">0.9.2.</span> <span class="nav-text">try-finally 语句</span></a><ol class="nav-child"><li class="nav-item nav-level-6"><a class="nav-link" href="#实例-8"><span class="nav-number">0.9.2.1.</span> <span class="nav-text">实例</span></a></li></ol></li><li class="nav-item nav-level-5"><a class="nav-link" href="#触发异常"><span class="nav-number">0.9.3.</span> <span class="nav-text">触发异常</span></a><ol class="nav-child"><li class="nav-item nav-level-6"><a class="nav-link" href="#实例-9"><span class="nav-number">0.9.3.1.</span> <span class="nav-text">实例</span></a></li></ol></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#面向对象"><span class="nav-number">0.10.</span> <span class="nav-text">面向对象</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#基础重载方法"><span class="nav-number">0.10.1.</span> <span class="nav-text">基础重载方法</span></a></li></ol></li></ol></li></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">ananzhusen</span>

  
</div>

<!-- 
  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.4</div>
 -->



        
<div class="busuanzi-count">
  <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="site-uv">
      本站访客数 <i class="fa fa-user"></i>
      <span class="busuanzi-value" id="busuanzi_value_site_uv"></span>
      人次
    </span>
  

  
    <span class="site-pv">
      本站访问总量 <i class="fa fa-eye"></i>
      <span class="busuanzi-value" id="busuanzi_value_site_pv"></span>
      次
    </span>
  
</div>








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  











  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  

  
  
    <script type="text/javascript" src="/lib/canvas-nest/canvas-nest.min.js"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  










  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  <script src="//unpkg.com/valine/dist/Valine.min.js"></script>
  
  <script type="text/javascript">
    var GUEST = ['nick','mail','link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item=>{
      return GUEST.indexOf(item)>-1;
    });
    new Valine({
        el: '#comments' ,
        verify: false,
        notify: false,
        appId: 'e69lyQSoCtoaLGljT3kXLBEQ-gzGzoHsz',
        appKey: 'pRDcFfyQnO4jQWxElUFupJ2D',
        placeholder: 'Just go go',
        avatar:'mm',
        guest_info:guest,
        pageSize:'10' || 10,
    });
  </script>



  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  

  

  
  

  

  

  


  
   <canvas class="fireworks" style="position: fixed;left: 0;top: 0;z-index: 1; pointer-events: none;"></canvas> 
   <script type="text/javascript" src="//cdn.bootcss.com/animejs/2.2.0/anime.min.js"></script> 
   <script type="text/javascript" src="/js/src/fireworks.js"></script>
  

</body>
</html>
